<template>
  <!-- 一条文章单元格 -->
  <van-cell @click.stop="$router.push(`/article_detail?aid=${obj.art_id}`)">
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ obj.title }}</span>
        <img
          v-if="obj.cover.type === 1"
          class="thumb"
          :src="obj.cover.images[0]"
        />
      </div>
      <div class="thumb-box" v-if="obj.cover.type > 1">
        <img
          v-for="(imgUrl, index) in obj.cover.images"
          :key="index"
          class="thumb"
          :src="imgUrl"
        />
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{ obj.aut_name }}</span>
          <span>{{ obj.comm_count }}</span>
          <span>{{ forMatTime(obj.pubdate) }}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon
          name="cross"
          @click.stop="show1 = true"
          v-show="isShowCross"
        />
        <van-action-sheet
          v-model="show1"
          :actions="actions1"
          @select="onSelect"
          get-container="body"
          cancel-text="取消"
        />
        <van-action-sheet
          v-model="show2"
          :actions="actions2"
          @select="onSelect"
          get-container="body"
          cancel-text="取消"
        />
        <!-- 反馈问题对话框 -->
        <van-dialog
          v-model="show3"
          title="反馈其它问题"
          show-cancel-button
          get-container="body"
          :beforeClose="onBeforeClose"
        >
          <van-field
            v-model.trim="msg"
            v-jujiao
            type="textarea"
            placeholder="请输入反馈的内容1-100字"
            border
          />
        </van-dialog>
      </div>
    </template>
  </van-cell>
</template>

<script>

import { firstActions, secondActions } from '@/constent'
import { Toast } from 'vant'
import { articleDislikeAPI, articleReportsAPI } from '@/api/home'
export default {
  name: 'ArticleItem',
  data () {
    return {
      msg: '',
      show1: false,
      show2: false,
      show3: false,
      actions1: firstActions,
      actions2: secondActions

    }
  },
  props: {
    // 文章对象
    obj: {
      type: Object,
      required: true
    },
    // 文章对象 在数组中的下标
    i: Number,
    // 控制X的可见性
    isShowCross: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    async onBeforeClose (action, done) {
      if (action === 'confirm') {
        if (this.msg.length === 0) {
          Toast('请输入反馈内容')
          done(false)
        } else {
          done()
          try {
            await articleReportsAPI({
              target: this.obj.art_id,
              type: '0',
              remark: this.msg
            })
            Toast.success('反馈成功')
          } catch (err) {
            Toast.success(err.message || '反馈失败')
          }
        }
      } else {
        done()
      }
      this.msg = ''
    },

    // 反馈面板点击事件
    async onSelect (actions1) {
      // action绑定的数据对象, index是索引
      console.log(actions1)
      if (actions1.id === 22) {
        // 证明点击的是反馈垃圾内容
        this.show2 = true
      } else if (actions1.id === 11) {
        this.$emit('del-list', this.i)
        try {
          await articleDislikeAPI({
            target: this.obj.art_id
          })

          Toast.success('操作成功')
        } catch (err) {
          Toast.fail('操作失败')
        }
      } else {
        if (actions1.id === 0) {
          this.show3 = true
        } else {
          try {
            await articleReportsAPI({
              target: this.obj.art_id,
              type: actions1.id + '',
              remark: '用户输入的原因' || actions1.name
            })
            Toast.success('反馈成功')
          } catch (err) {
            Toast.fail(err.message || '反馈失败')
          }
        }
        this.show1 = this.show2 = false
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
